﻿using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Sino.CapacityCloud.Repositories.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace Sino.CapacityCloud.WebApi.Utilities
{

    public static class QueryExtensions
    {

        public static bool HasKeyword(this string p, string keyword)
        {
            if (string.IsNullOrEmpty(p))
            {
                return false;
            }
            return p.Contains(keyword);
        }

        public static bool DefaultUserFilter(this ApplicationUser x, string keyword, string carCode, string roleId, CertificationStatus state, DateTime? startTime, DateTime? endTime)
        {
            if (!string.IsNullOrEmpty(keyword))
            {
                if (!(HasKeyword(x.RealName, keyword) ||
                    HasKeyword(x.PhoneNumber, keyword) ||
                    HasKeyword(x.UserName, keyword)))
                {
                    return false;
                }
            }
            if (!string.IsNullOrEmpty(carCode))
            {
                if (!HasKeyword(x.Car?.Code, carCode))
                {
                    return false;
                }
            }
            if (!string.IsNullOrEmpty(roleId))
            {
                if (!x.Roles.Any(z => z.RoleId == roleId))
                {
                    return false;
                }
            }
            if (state != CertificationStatus.None)
            {
                if (x.IdentityCardCertificationState != state)
                {
                    return false;
                }
            }
            if (startTime != null && endTime != null)
            {
                if (!(x.CertificationTime >= startTime && x.CertificationTime <= endTime))
                {
                    return false;
                }
            }
            return true;
        }

    }

}